package com.dada.tiku.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.dada.tiku.dao.mapper.DicCategoryMapper;
import com.dada.tiku.dao.mapper.DicItemMapper;
import com.dada.tiku.dao.mapper.DicUserMapper;
import com.dada.tiku.dao.mapper.TUserMapper;
import com.dada.tiku.pojo.DicCategory;
import com.dada.tiku.pojo.DicCategoryExample;
import com.dada.tiku.pojo.DicItem;
import com.dada.tiku.pojo.DicItemExample;
import com.dada.tiku.pojo.DicUser;
import com.dada.tiku.pojo.DicUserExample;
import com.dada.tiku.pojo.Subject;
import com.dada.tiku.pojo.DicUserExample.Criteria;
import com.dada.tiku.pojo.TUser;
import com.dada.tiku.pojo.TUserExample;
import com.dada.tiku.service.DictionaryService;
import com.dada.tiku.utils.WxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

//服务是用dubbo发布的，@Service 使用dubbo包中的
@Service
public class DictionaryServiceImpl implements DictionaryService {

	@Autowired
	private DicItemMapper dicItemMapper;

	public WxResult selectAllDicts(Integer start, Integer count) {
		// 在查询之前先开启分页
		PageHelper.startPage(start,count);

		DicItemExample example = new DicItemExample();
		// example.setOrderByClause("id asc"); mybatis逆向工程排序
		example.setOrderByClause("id asc");
		com.dada.tiku.pojo.DicItemExample.Criteria c = example.createCriteria();
		c.andStatusEqualTo("1");
		// 这里查出来的应该实现分组查询，但是逆向工程没有提供--只能在前台处理
		List<DicItem> dictItemsList = dicItemMapper.selectByExample(example);
		
		PageInfo<DicItem> info = new PageInfo<>(dictItemsList);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(dictItemsList);
		//将总共查到总条数封装到msg中,但是这里是字符串格式
		result.setMsg(info.getTotal()+"");
		
		return result;

	}

	// 查找词汇详情
	public DicItem findDictDetail(Integer id) {
		DicItem item = dicItemMapper.selectByPrimaryKey(id);
		return item;
	}

	/*
	 * 添加收藏 不管是收藏还是错题，都是多对多，所以不能直接放到用户表或者 词汇表，需要引入中间表
	 */
	@Autowired
	private DicUserMapper dicUserMapper;
	
	@Autowired
	private TUserMapper userMapper;
	
	public WxResult connectionDic(Integer id, String openid) {
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		// 这里就不对id进行判断了，前台传递过来修改数据库的都走post请求
		// 查询dicUserMapper 有没有数据，判断是不是第一次收藏
		DicUserExample dicexample = new DicUserExample();
		Criteria criteria = dicexample.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andDicidEqualTo(id);
		List<DicUser> diclist = dicUserMapper.selectByExample(dicexample);
		if (diclist == null || diclist.isEmpty() == true) {
			// 走到这里代表这是第一次收藏，添加一条数据
			DicUser dicUser = new DicUser();
			dicUser.setDicid(id);
			dicUser.setUserid(userid);
			dicUser.setCollection("1");// 设置收藏
			dicUser.setStatus("1");// 设置状态为启用
			//将数据添加到数据库中
			int row = dicUserMapper.insert(dicUser);
			if(row != 1){
				//代表插入数据失败
				System.out.println("添加收藏插入数据失败----");
				return WxResult.build(400, "添加收藏插入数据失败");
			}
			return WxResult.ok();
		} else {
			// 代表着不是第一次收藏，修改状态即可
			DicUser dicUser = diclist.get(0);
			dicUser.setCollection("1");
			int row = dicUserMapper.updateByPrimaryKey(dicUser);
			if(row !=1){
				System.out.println("添加收藏修改数据失败----");
				return WxResult.build(400, "添加收藏修改数据失败");
			}
			return WxResult.ok();
		}

	}

	/*
	 * 添加错题
	 */
	public WxResult addErrorDic(Integer dicid, String openid) {
		
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		DicUserExample example = new DicUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andDicidEqualTo(dicid);
		List<DicUser> diclist = dicUserMapper.selectByExample(example);
		if (diclist == null || diclist.isEmpty() == true) {
			// 第一次添加错题
			DicUser dicUser = new DicUser();
			dicUser.setDicid(dicid);
			dicUser.setUserid(userid);
			dicUser.setIsright("0");
			dicUser.setStatus("1");// 设置状态为启用
			//将数据添加到数据库中
			int row = dicUserMapper.insert(dicUser);
			if(row != 1){
				//代表插入数据失败
				System.out.println("添加错题插入数据失败----");
				return WxResult.build(400, "添加错题插入数据失败");
			}
			return WxResult.ok();
		} else {
			// 代表着不是第一次收藏，修改状态即可
			DicUser dicUser = diclist.get(0);
			dicUser.setIsright("0");
			int row = dicUserMapper.updateByPrimaryKey(dicUser);
			if(row !=1){
				System.out.println("添加收藏修改数据失败----");
				return WxResult.build(400, "添加收藏修改数据失败");
			}
			
			return WxResult.ok();
		}

	}

	/*
	 * 移除错题的方法
	 */
	public WxResult removeErrorDic(Integer dicid, String openid) {
		
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		// 移除错题的方法，既然可以移除代表数据库有记录，直接改值
		DicUserExample example = new DicUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andDicidEqualTo(dicid);
		criteria.andUseridEqualTo(userid);
		List<DicUser> selectByExample = dicUserMapper.selectByExample(example);
		List<DicUser> dicusers = selectByExample;
		DicUser dicUser = dicusers.get(0);
		dicUser.setIsright("1");// 设置是否正确为1.代表正确
		// 更新表---这样似乎就可以直接改
		dicUserMapper.updateByPrimaryKey(dicUser);
		return WxResult.ok();
	}

	/*
	 * 取消收藏的方法和移除托提类似
	 */
	public WxResult cancelConnection(Integer dicid, String openid) {
		
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		DicUserExample example = new DicUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andDicidEqualTo(dicid);
		criteria.andUseridEqualTo(userid);
		List<DicUser> selectByExample = dicUserMapper.selectByExample(example);
		List<DicUser> dicusers = selectByExample;
		DicUser dicUser = dicusers.get(0);
		dicUser.setCollection("0");// 设置0 代表取消收藏，默认null的代表没有点过
		// 更新表---这样似乎就可以直接改
		dicUserMapper.updateByPrimaryKey(dicUser);
		return WxResult.ok();
	}

	@Autowired
	private DicCategoryMapper categoryMapper;

	/*
	 * 按照分类查询词汇
	 */
	public List<DicItem> queryByCategory(String categoryname, Integer start, Integer count) {
		// 知道了分了名称，先找到分类id
		DicCategoryExample example1 = new DicCategoryExample();
		com.dada.tiku.pojo.DicCategoryExample.Criteria createCriteria = example1.createCriteria();
		createCriteria.andTitleEqualTo(categoryname);
		// 查询cid
		List<DicCategory> categories = categoryMapper.selectByExample(example1);
		Integer cid = categories.get(0).getCid();
		// 查询分类下词典集合-- 查之前设置分页条件
		PageHelper.startPage(start, count);
		DicItemExample example = new DicItemExample();
		example.setOrderByClause("itemname");
		com.dada.tiku.pojo.DicItemExample.Criteria criteria = example.createCriteria();
		criteria.andCidEqualTo(cid);
		List<DicItem> dicItems = dicItemMapper.selectByExample(example);
		return dicItems;
	}

	/*
	 * 通过关键字查询
	 */
	public WxResult queryDictByKeywords(String keywords, Integer start, Integer count) {
		// 查询分类下词典集合-- 查之前设置分页条件
		PageHelper.startPage(start, count);
		DicItemExample example = new DicItemExample();
		example.setOrderByClause("id asc");
		com.dada.tiku.pojo.DicItemExample.Criteria criteria = example.createCriteria();
		//TODO --这里应该设置多字段模糊查询
		criteria.andItemnameLike("%"+keywords+"%");
		List<DicItem> dicItems = dicItemMapper.selectByExample(example);
		//这里不对结果做处理，查出什么就是什么
		
		PageInfo<DicItem> info = new PageInfo<>(dicItems);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(dicItems);
		//将总共查到总条数封装到msg中,但是这里是字符串格式
		result.setMsg(info.getTotal()+"");
		
		return result;
		
	}

	/*
	 * 查询用户收藏的词汇集合列表,不需要判断,有就有没有就没有
	 * @see com.dada.tiku.service.DictionaryService#getConnectionList(java.lang.String)
	 */
	public WxResult getConnectionList(String openid,Integer start, Integer count) {
		
		//先根据openid查找用户id
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		// 在查询之前先开启分页
		PageHelper.startPage(start,count);
		
		//查找dic_user表--条件是用户id and collection =1
		DicUserExample example = new DicUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andCollectionEqualTo("1");
		List<DicUser> connlist = dicUserMapper.selectByExample(example);
		//根据查询出来的集合,遍历封装一个List<DicItem>
		List<DicItem> list = new ArrayList<DicItem>();
		for (DicUser dicUser : connlist) {
			Integer dicid = dicUser.getDicid();
			//根据dicid查找对应的词汇项对象,然后在封装到list中
			DicItem item = dicItemMapper.selectByPrimaryKey(dicid);
			list.add(item);
		}
		//不需要做处理,直接返回

		PageInfo<DicUser> info = new PageInfo<>(connlist);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(list);
		//将总共查到总条数封装到msg中,但是这里是字符串格式
		result.setMsg(info.getTotal()+"");
		
		return result;
	}

	/*
	 * 查询用户错题的
	 * @see com.dada.tiku.service.DictionaryService#getErrorList(java.lang.String)
	 */
	public WxResult getErrorList(String openid,Integer start, Integer count) {
		
		
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		// 在查询之前先开启分页
		PageHelper.startPage(start,count);
				
		//查找dic_user表--条件是用户id and isright = 0
				DicUserExample example = new DicUserExample();
				Criteria criteria = example.createCriteria();
				criteria.andUseridEqualTo(userid);
				criteria.andIsrightEqualTo("0");
				List<DicUser> cerrorlist = dicUserMapper.selectByExample(example);
				//根据查询出来的集合,遍历封装一个List<DicItem>
				List<DicItem> list = new ArrayList<DicItem>();
				for (DicUser dicUser : cerrorlist) {
					Integer dicid = dicUser.getDicid();
					//根据dicid查找对应的词汇项对象,然后在封装到list中
					DicItem item = dicItemMapper.selectByPrimaryKey(dicid);
					list.add(item);
				}
				//不需要做处理,直接返回
				PageInfo<DicUser> info = new PageInfo<>(cerrorlist);
//				System.out.println(info.getTotal());
				WxResult result = new WxResult();
				result.setData(list);
				//将总共查到总条数封装到msg中,但是这里是字符串格式
				result.setMsg(info.getTotal()+"");
				
				return result;
	}

	/*
	 * 判断用户该词汇是否收藏
	 * @see com.dada.tiku.service.DictionaryService#isCollection(java.lang.Integer)
	 */
	public WxResult isCollection(Integer id, String openid) {
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		DicUserExample example = new DicUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andDicidEqualTo(id);
		List<DicUser> list = dicUserMapper.selectByExample(example);
		if(list.isEmpty() || list.get(0).getCollection() == null ||Integer.parseInt(list.get(0).getCollection()) ==0){
			//代表用户没收藏或者没收藏过
			return WxResult.build(300, "用户没有收藏该词汇");
		}
		return WxResult.build(200, "用户收藏了该词汇");
	}
	
	/*
	 * 判断用户是否添加该词汇为错题
	 */
	public WxResult isError(Integer id, String openid) {
		TUserExample userexample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userexample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//先根据openid查找userid
		List<TUser> users = userMapper.selectByExample(userexample);
		//前台有openid的话代表用户已经在数据库中有记录了,直接取就行
		Integer userid = users.get(0).getId();
		
		DicUserExample example = new DicUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andDicidEqualTo(id);
		List<DicUser> list = dicUserMapper.selectByExample(example);
		if(list.isEmpty() || list.get(0).getIsright() == null ||Integer.parseInt(list.get(0).getIsright()) ==1){
			//代表用户没添加或者添加过该词汇为错题
			return WxResult.build(300, "用户没有添加该词汇为错题");
		}
		return WxResult.build(200, "用户添加了该词汇为错题");
	}

}
